Page({

    data: {
        goods: null,
        img: '../../../image/uploadimg.png',
        allPrice: null,
        orderId: null,
        option: ['.mid-bto-right-none', '.mid-bto-right-none'],
        num: null,
        userID: null
    },

    onLoad (query) {
        let data = JSON.parse(query.data);
        let orderId = JSON.parse(query.orderId);
        console.log(data);
        console.log(orderId);  
        this.setData({
            goods: data,
            orderId: orderId  
        });
        let allPrice = this.data.goods.detailPrice;
        this.setData({
            allPrice: allPrice
        })
    },

    changeimg () {
        let that = this;
        wx.showActionSheet({
            itemList: ["从相册选择", "拍照"],
            success(res) {
                if (res.tapIndex == 0) {
                    wx.chooseImage({
                        count: 1,
                        sizeType: ['original', 'compressed'],
                        sourceType: ['album'],
                        success: function (res) {
                            console.log(res.tempFilePaths);
                            that.setData({
                                img: res.tempFilePaths[0]
                            })
                        }
                    })
                } else {
                    wx.chooseImage({
                        count: 1,
                        sizeType: ['original', 'compressed'],
                        sourceType: ['camera'],
                        success: function (res) {
                            const tempFilePaths = res.tempFilePaths;
                            console.log(res.tempFilePaths);
                            that.setData({
                                img: res.tempFilePaths[0]
                            })
                        }
                    })
                }
            }
        })
    },

    handleClick (e) {
        console.log(e.currentTarget.dataset.index);
        let sum = e.currentTarget.dataset.index;
        let arr = [];
        if (sum == '1') {
            arr[0] = '.mid-bto-right-selected',
            arr[1] = '.mid-bto-right-none'
        } else {
            arr[0] = '.mid-bto-right-none',
            arr[1] = '.mid-bto-right-selected'
        }
        this.setData({
            option: arr,
            num: sum            
        })
        console.log(this.data.option);
    },

    formSubmit (e) {
        console.log('ok');
        console.log(e);
        console.log(this.data.img);
        let that = this;
        wx.getStorage({
            key: 'userID',
            success(res) {
                that.setData({
                    userID: res.data
                }) 
            }
        });
        let userId = this.data.userID;
        if (userId == null) {
            userId = getApp().globalData.userID
        }
        console.log(this.data.num);
        console.log(this.data.allPrice);
        console.log(e.detail.value.text);
        wx.request({
            url: 'https://localhost:80/afterSale',
            method: 'post',
            data: {
                orderId: that.data.orderId,
                orderdetId: that.data.goods.detailId,
                userId: userId,
                goodsId: that.data.goods.goods.goodsId,
                goodsName: that.data.goods.goods.goodsName,
                count: that.data.goods.detailNum,
                goodsPrice: that.data.goods.goods.goodsPrice,
                money: that.data.allPrice,
                questType: that.data.num,
                questDesc: e.detail.value.text,
                questImgs: that.data.img
            },
            success (res) {
                console.log(res);
                wx.navigateTo({
                    url: '../application/application'
                });
                wx.request({
                    url: 'https://www.zhaoshenma.cn:80/order/changeOrderStatus', 
                    data: {
                      orderId: that.data.orderId,
                      orderState: '5'
                    },
                    method : 'POST',
                    success: function (res) {
                      console.log('成功');
                    },
                    fail: function () {
                      console.log('请求失败！');
                    }
                })  
            },
            error (res) {
                console.log(res);
                wx.showModal({
                    title: '发送失败',
                    content: '可能因为网络的原因提交出错，请重新提交',
                    text:'center',
                    complete() {}
                });
            }
        })
    }
})